package apps.softtech.CustomView;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import apps.softtech.Model.Tag;
import apps.softtech.Model.ViewLoadItem;
import apps.softtech.Utility.FileUtility;

public class ViewLoadContentRequireFieldProduct extends LinearLayout {

	public static final int MODE_EDIT_TEXT = 0;
	public static final int MODE_IMAGE_VIEW = 1;
	
	private Context mContext;
	
	private ProgressBar progressLoading;
	
	private ArrayList<ViewLoadItem> listLoader;
	
	/**
	 * constructor view
	 * show dialog when add view
	 * @param context
	 * @param attrs
	 */
	public ViewLoadContentRequireFieldProduct(Context context, AttributeSet attrs){
		super(context , attrs);
		mContext = context;
		this.setOrientation(LinearLayout.VERTICAL);
		showProgressbar(mContext);
	}
	
	/**
	 * Check progress bar is show ing
	 * @return progress bar visibility status
	 */
	public Boolean isShowProgressbar(){
		return !(progressLoading==null);
	}
	
	/**
	 * Show progress bar and remove all view
	 * @param context
	 */
	public void showProgressbar(Context context){
		if(this.getChildCount() > 0){
			this.removeAllViews();
		}
		progressLoading = new ProgressBar(context);
		LayoutParams param = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT );
		param.gravity = Gravity.CENTER_VERTICAL;
		
		
		progressLoading.setLayoutParams(param);
		
		this.addView(progressLoading);
		
	}
	
	/**
	 * hide progress bar
	 */
	public void hideProgressbar(){
		this.removeView(progressLoading);
		progressLoading.destroyDrawingCache();
		progressLoading = null;
	}
	
	/**
	 * add layer from array
	 */
	public void addLayerRequireFromArray(){
		for(int i = 0 ; i < listLoader.size() ; i ++ ){
			addLayer(listLoader.get(i));
		}
	}
	
	/**
	 * add layer from item
	 * @param item
	 */
	public void addLayer(ViewLoadItem item){
		
		switch (item.getTag().getTagValueType()) {
		case MODE_EDIT_TEXT:
			addLayerTypeEditText(item);
			break;
		case MODE_IMAGE_VIEW:
			addLayerTypeImageView(item);
			break;
		case 2:
			break;
		default:
			break;
		}
		
	}
	
	/**
	 * add layer type edittext
	 * @param item
	 */
	public void addLayerTypeEditText(ViewLoadItem item){
		EditText edtForTag = new EditText(mContext);
		LayoutParams param = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		edtForTag.setLayoutParams(param);
		edtForTag.setTag(item.getTag());
		edtForTag.setHint(item.getTag().getTagDescription());
		item.setView(edtForTag);
		this.addView(edtForTag);
	}
	
	/**
	 * add layer type image view
	 * @param item
	 */
	public void addLayerTypeImageView(ViewLoadItem item){
		LinearLayout linearImage = new LinearLayout(mContext);
		LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		params.gravity = Gravity.CENTER_VERTICAL;
		linearImage.setLayoutParams(params);
		linearImage.setOrientation(LinearLayout.HORIZONTAL);
		
		TextView tvDescription = new TextView(mContext);
		params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		tvDescription.setLayoutParams(params);
		tvDescription.setText(item.getTag().getTagDescription());
		
		ImageView imgContentImage = new ImageView(mContext);
		params = new LayoutParams(100, 100);
		imgContentImage.setLayoutParams(params);
		imgContentImage.setBackgroundColor(Color.YELLOW);
		imgContentImage.setMinimumHeight(100);
		imgContentImage.setMinimumWidth(100);
		
		linearImage.addView(tvDescription);
		linearImage.addView(imgContentImage);
		
		imgContentImage.setTag(item.getTag());
		item.setView(imgContentImage);
		this.addView(linearImage);
		
	}

	public ArrayList<ViewLoadItem> getListLoader() {
		return listLoader;
	}

	public void setListLoader(ArrayList<ViewLoadItem> listLoader) {
		this.listLoader = listLoader;
	}
	
	/**
	 * set list loader from a json array
	 * @param array: not null and correct form of json array
	 */
	public void setListLoaderFromJSONArray(JSONArray array){
		listLoader = new ArrayList<ViewLoadItem>();
		try{
			Tag tagItem = null;
			ViewLoadItem viewLoadItem = null;
			for(int i=0 ; i < array.length() ; i++){
				try{
					tagItem = new Tag();
					tagItem.setTagFromJSON(array.getJSONObject(i));
					viewLoadItem = new ViewLoadItem(tagItem );
					viewLoadItem.setRequireField(array.getJSONObject(i).getInt("st_IsRequiredField") == 1);
					listLoader.add(viewLoadItem);
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
	
	/**
	 * check list loader day du cac require field
	 * @return
	 */
	public Boolean checkValidatePairListResult(){
		for(int i = 0 ; i < listLoader.size() ; i ++ ){
			if(listLoader.get(i).isRequireField() && !FileUtility.checkStringNotNull(listLoader.get(i).getValue())){
				return false;
			}
		}
		return true;
	}
	
	/**
	 * pair list require field to json
	 * @return
	 */
	public JSONObject pairListToJSON(){
		JSONObject pairList = new JSONObject();
		
		try{
			for(int i = 0 ; i < listLoader.size() ; i++){
				try{
					pairList.put(String.valueOf(listLoader.get(i).getTag().getTagId()), listLoader.get(i).getValue());
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return pairList;
	}
	
}
